//
//  DetailTableViewController.m
//  bh
//
//  Created by lanou on 15/12/18.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "DetailTableViewController.h"
#import "LORequestManger.h"
#import "Photos.h"
#import "PhotosTableViewCell.h"
#import "BrowseThreeTableViewCell.h"
#import "DetailTowTableViewController.h"
#import "DetailViewController.h"
#import "ProfessorModel.h"

@interface DetailTableViewController ()<UITableViewDelegate,UITableViewDataSource>
// 存放model的数组
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, assign) NSInteger indexPath;
@property (nonatomic, strong) BrowseModel *browseModel;

@end

@implementation DetailTableViewController

- (NSMutableArray *)array {
    if (!_array) {
        _array = [[NSMutableArray alloc]init];
    }
    return _array;
}

// 详情界面解析
- (void)setData {
    // url
    NSString *string = [NSString stringWithFormat:@"http://api.meilijia.com/app/featured_subject?subject_id=%@",self.idString];
    [LORequestManger GET:string success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        NSDictionary *data = dic[@"data"];
        NSDictionary *featured_subject = data[@"featured_subject"];
        NSDictionary *info = featured_subject[@"info"];
        NSArray *collections = info[@"collections"];
        for (NSDictionary *dict in collections) {
            if ([self.panDuan isEqualToString:@"灵感"]) {
                Photos *photoModel = [[Photos alloc]init];
                [photoModel setValuesForKeysWithDictionary:dict];
                [self.array addObject:photoModel];
            }
            if ([self.panDuan isEqualToString:@"设计"]) {
                BrowseModel *browseModel = [[BrowseModel alloc]init];
                [browseModel setValuesForKeysWithDictionary:dict];
                [self.array addObject:browseModel];
            }
        }
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self setData];
    self.navigationItem.title = self.itemTitle;
    // 注册单元格
    // 上大图下label
    [self.tableView registerClass:[PhotosTableViewCell class] forCellReuseIdentifier:@"cell1"];
    // 左图右label
    [self.tableView registerClass:[BrowseThreeTableViewCell class] forCellReuseIdentifier:@"cell2"];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.panDuan isEqualToString:@"灵感"]) {
        PhotosTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
        cell.photos = self.array[indexPath.row];
        NSLog(@"%@",cell.photos.author_id);
        return cell;
    }
    if ([self.panDuan isEqualToString:@"设计"]) {
        BrowseThreeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
        cell.browseModel = self.array[indexPath.row];
        cell.browseModel = self.array[indexPath.row];
        UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotoPersonalDetail:)];
        [cell.personalPictureView addGestureRecognizer:tap2];
        UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotoDetail:)];
        // 添加收藏
        UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shoucang:)];
        [cell.likeImageView addGestureRecognizer:tap3];
        cell.likeImageView.tag = indexPath.row;
        cell.pictureView.tag = indexPath.row;
        cell.personalPictureView.tag = indexPath.row;
        [cell.pictureView addGestureRecognizer:tap1];
        return cell;
    }
    return nil;
}

// 收藏按钮
- (void)shoucang:(UITapGestureRecognizer *)sender {
    NSLog(@"123");
}
// 图片的点击事件
- (void)gotoDetail:(UITapGestureRecognizer *)sender {
    _browseModel = _array[sender.view.tag];
    DetailTowTableViewController *detailTwoVC = [[DetailTowTableViewController alloc]init];
    
    detailTwoVC.browseModel = self.browseModel;
    
    [self.navigationController pushViewController:detailTwoVC animated:YES];
    
}

- (void)gotoPersonalDetail:(UITapGestureRecognizer *)sender{
    
    DetailViewController *detailVC  = [[DetailViewController alloc] init];
    
    _browseModel = _array[sender.view.tag];
    ProfessorModel *model = [[ProfessorModel alloc]init];
    model.user_face = _browseModel.author_face;
    model.contacted_count = _browseModel.item_count;
    model.verified_work_count = _browseModel.view_count;
    model.location = _browseModel.title_sub_1;
    model.user_nick = _browseModel.author_nick;
    model.user_id = _browseModel.author_id;
    model.pro_price_min = _browseModel.buylist_total;
    model.pro_price_max = _browseModel.work_design_price;
    detailVC.professorModel = model;
    
    
    
    
    [self presentViewController:detailVC animated:YES completion:nil];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.panDuan isEqualToString:@"灵感"]) {
       return 140; 
    }if ([self.panDuan isEqualToString:@"设计"]){
     return kHeight / 2 + 110;
    }
    return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.panDuan isEqualToString:@"灵感"]) {
        
        
        NSLog(@"%@",_browseModel.title);
        DetailTowTableViewController *detailTwoVC = [[DetailTowTableViewController alloc]init];
        Photos *photoModel = _array[indexPath.row];
        detailTwoVC.browseModel = [[BrowseModel alloc]init];
        detailTwoVC.browseModel.cover_pic = photoModel.cover_pic;
        detailTwoVC.browseModel.author_face = photoModel.author_face;
        detailTwoVC.browseModel.ID = photoModel.ID;
        detailTwoVC.browseModel.title = photoModel.title;
        detailTwoVC.browseModel.title_sub = photoModel.title_sub_3;
        [self.navigationController pushViewController:detailTwoVC animated:YES];
        NSLog(@"11");
    }
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
